//
//  FUNCallViewController.m
//  WeCloudChatDemo
//
//  Created by WeCloudIOS on 2022/4/25.
//  Copyright © 2022 WeCloud. All rights reserved.
//


#import "IMSDKDemo-Swift.h"
#import "CallViewController.h"
#import "WeCloudIMSDK/WeCloudIMSDK.h"

@interface CallViewController ()
@property (weak, nonatomic) IBOutlet UIButton *answerBtn;
@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

@end

@implementation CallViewController

+ (instancetype)shared{
    static CallViewController *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[CallViewController alloc] init];
    });
    return instance;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    if (self.isCall) {
        self.cancelBtn.hidden = NO;
        self.answerBtn.hidden = self.refuseBtn.hidden = YES;
    }else{
        self.cancelBtn.hidden = YES;
        self.answerBtn.hidden = self.refuseBtn.hidden = NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    self.isCall = NO;
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isInRoom"];
    for (UIView *view  in self.view.subviews) {
        if (view.tag != 1001) {
            [view removeFromSuperview];
        }
    }
}


- (IBAction)answer:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    [[WeCloudIMClient sharedWeCloudIM] wcimAcceptWithRoomId:self.roomID callback:^(NSString * _Nonnull reqId, id  _Nullable responseData, NSError * _Nullable error, NSInteger responseCode) {
        if (!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf insertRoom];
            });
        }
    }];

}
- (IBAction)refuse:(UIButton *)sender {
    
    [[WeCloudIMClient sharedWeCloudIM] wcimRefuseWithRoomId:self.roomID callback:^(NSString * _Nonnull reqId, id  _Nullable responseData, NSError * _Nullable error, NSInteger responseCode) {
        if (!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
            
        }
    }];
}
- (IBAction)cancel:(UIButton *)sender {
    
    [[WeCloudIMClient sharedWeCloudIM] wcimRefuseWithRoomId:self.roomID callback:^(NSString * _Nonnull reqId, id  _Nullable responseData, NSError * _Nullable error, NSInteger responseCode) {
        if (!error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        }
    }];
}

-(void)insertRoom{

    Answer *answer = [[Answer alloc]init];
    [answer openWithVC:self roomID:self.roomID token:self.token];
}


@end
